<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>文档管理</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
    <link rel="stylesheet" href="/static/ui/css/ui.css" />
</head>

<body class="pear-container">
    <div class="layui-btn-container" style="display: inline-block;">
        <div class="layui-btn-group">
            <button class="layui-btn layui-btn-radius layui-btn-normal" id="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <!--button class="layui-btn layui-btn-radius layui-btn-normal" id="back">
                <i class="layui-icon layui-icon-senior"></i>
                md2mysql
            </button-->
            <button class="layui-btn layui-btn-radius layui-btn-normal" id="downdoc">
                <i class="layui-icon layui-icon-app"></i>
                预览/下载
            </button>
            <button class="layui-btn layui-btn-radius layui-btn-normal" id="view">
                <i class="layui-icon layui-icon-camera"></i>
                查看
            </button>
        </div>
    </div>
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="power-table" lay-filter="power-table"></table>
        </div>
    </div>


    <script type="text/html" id="power-bar">
            {{# if(d.type == 1){ }}
            <button class="pear-btn pear-btn-success pear-btn-sm" lay-event="share">
                <i class="layui-icon layui-icon-share"></i>
            </button>
            <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
                <i class="layui-icon layui-icon-edit"></i>
            </button>
            <button class="pear-btn pear-btn-sm" lay-event="viewone">
                <i class="layui-icon layui-icon-camera"></i>
            </button>
            {{# } }}
            <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
                <i class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
    <script type="text/html" id="doc-time">
			{{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
		</script>
    <script type="text/html" id="up-time">
			{{layui.util.toDateString(d.update_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
		</script>
    <script src="../config.js"></script>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/ui/ui.js"></script>
    <script>
        layui.use([
            'table', 'form', 'jquery', 'treeTable'
        ], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let treeTable = layui.treeTable,
                req = _req();

            let MODULE_PATH = "./";

            treeTable.render({
                tree: {
                    iconIndex: 1,  // 折叠图标显示在第几列
                    idName: 'id',  // 自定义id字段的名称
                    pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
                    haveChildName: 'have_child',  // 自定义标识是否还有子节点的字段名称
                    isPidData: true  // 是否是pid形式数据
                },
                //skin: 'line',
                toolbar: '#power-toolbar',
                elem: '#power-table',
                //url: 'doc/listmd?did=' + req.did,
                reqData: function (data, callback) {
                    // 在这里写ajax请求，通过callback方法回调数据
                    console.log(data)
                    let pid = data ? data.id : 0;
                    _get(layui, 'doc/listmd?did=' + req.did + '&pid=' + pid, function (res) {
                        console.log(res)
                        callback(res);  // 参数是数组类型
                    });
                },
                //page: false,
                cols: [

                    {
                        field: 'id',
                        title: 'ID'
                    }, {
                        field: 'title',
                        width: 200,
                        edit: 'text',
                        title: '名称'
                    }, {
                        field: 'order_num',
                        minWidth: 90,
                        edit: 'text',
                        title: '排序'
                    }, {
                        field: 'name',
                        title: '路径'
                    }, {
                        title: '添加时间',
                        field: 'add_time',
                        align: 'center',
                        templet: '#doc-time'
                    },
                    {
                        title: '更新时间',
                        field: 'update_time',
                        align: 'center',
                        templet: '#up-time'
                    }, {
                        title: '操作',
                        templet: '#power-bar',
                        width: 200,
                        fixed : 'right',
                        align: 'center'
                    }

                ]
            });

            treeTable.on('tool(power-table)', function (obj) {
                if (obj.event === 'remove') {
                    window.remove(obj);
                } else if (obj.event === 'edit') {
                    window.edit(obj);
                } else if (obj.event === 'share') {
                    window.share(obj);
                } else if (obj.event === 'viewone') {
                    window.viewone(obj);
                }
            })
            window.share = function(obj) {
                //console.log(obj.data)
                layer.open({
                    type: 2,
                    title: '分享-' + obj.data.title,
                    shade: 0.1,
                    offset: 'rt',
                    area: ['90%', '100%'],
                    anim: 1,
                    maxmin: true,
                    content: `/res/group/share/touser.html?id=${obj.data.id}&type=doc_cate`
                });
            }
            window.viewone = function(obj) {
                window.open('/res/group/doc/view.html?id=' + obj.data.id);
            }
            treeTable.on('toolbar(power-table)', function (obj) {
                if (obj.event === 'add') {
                    window.addmd();
                }
                else if (obj.event === 'back') {
                    window.back();
                }
                else if (obj.event === 'view') {
                    window.open('/docs/' + req.file + '/index.html');
                }
                else if (obj.event === 'downdoc') {
                    window.open('/res/group/doc/down.html?did=' + req.did);
                }
                else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });
            treeTable.on('edit(power-table)', function (obj) {
                //console.log(obj)

                let update = {
                    id: obj.data.id,
                    field: obj.field,
                    value: obj.value
                }
                console.log(update)
                //return;
                _post(layui, 'doc/editData', update, res => {
                    //console.log(res)
                    layer.msg('更新成功', {
                        icon: 1,
                        time: 1000
                    });
                }, err => {
                    //console.log(err)

                })
            });
            $('#view').click(function () {
                window.open('/docs/' + req.file + '/index.html');
            })
            $('#downdoc').click(function () {
                window.open('/res/group/doc/down.html?did=' + req.did);
            })
            $("#add").click(function () {
                //parent.layui.admin.addTab('godocmsdocsadd_' + 113 + req.did,
                //'添加文档',
                //'/libs/view/doc/addmd.html?did=' + req.did)

                layer.open({
                    type: 2,
                    title: '添加文档',
                    shade: 0.1,
                    offset: 'rt',
                    area: ['98%', '100%'],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'addmd.html?did=' + req.did
                });
            })

            window.edit = function (obj) {
                parent.layui.admin.addTab('godocmsdocsedit_' + obj.data.id + 111,
                    obj.data.title + '-修改文档',
                    '/res/group/doc/editmd.html?id=' + obj.data.id + '&did=' + req.did)
                //console.log(obj)
                /*
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    offset: 'rt',
                    area: [
                        '98%', '100%'
                    ],
                    anim: 1,
                    maxmin: true,
                    content: MODULE_PATH + 'editmd.html?id=' + obj.data.id + '&did=' + req.did
                });*/
            }
            $("#back").click(function (obj) {
                layer.confirm('回写将按summary.md结构的md文件重置写入数据库！', {
                    icon: 3,
                    title: '提示'
                }, (index) => {
                    layer.close(index);
                    _post(layui, 'doc/back', {
                        did: req.did
                    }, res => {
                        layer.msg('回写成功', {
                            icon: 1,
                            time: 1000
                        }, () => {
                            render();
                        });
                    }, err => {
                        //layer.msg('删除失败',{icon:2,time:1000});
                    })
                });
            })

            window.remove = function (obj) {
                layer.confirm('确定要删除该文档', {
                    icon: 3,
                    title: '提示'
                }, (index) => {
                    layer.close(index);
                    _post(layui, 'doc/delmd', {
                        id: obj.data.id,
                        did: req.did
                    }, res => {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        }, () => {
                            obj.del();
                        });
                    }, err => {
                        //layer.msg('删除失败',{icon:2,time:1000});
                    })
                });
            }


        })
    </script>
</body>

</html>